/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.Controller;

import com.aptech.DAO.postHistoryDAO;
import com.aptech.DAO.postTypeDAO;
import com.aptech.DAO.realEstateDAO;
import com.aptech.DAO.userDAO;
import com.aptech.model.PostHistory;
import com.aptech.model.User;
import java.util.Calendar;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Hoang Quy Hong An
 */
public class insertPostHistory extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String ERROR = "error";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
         System.out.println("Cai thu 3");
        int userID;
        int postTypeID;
        String postTypePrice = "";
        int RealEstateID;
        String startDate;
        String endDate = "";
        boolean flag = false;
        postTypeDAO objPostTypeDAO;
        realEstateDAO objRealEstateDAO;
        postHistoryDAO objPostHistoryDAO;
        userDAO objUserDAO;
        try {
            objPostTypeDAO = new postTypeDAO();
            objRealEstateDAO = new realEstateDAO();
            objPostHistoryDAO = new postHistoryDAO();
            objUserDAO = new userDAO();
            User objUser = (User) request.getSession().getAttribute("user");
            userID = objUser.getUserID();
            postTypeID = (Integer) request.getSession().getAttribute("postTypeID");
            postTypePrice = objPostTypeDAO.getPostTypePriceByID(postTypeID);
            RealEstateID = (Integer) request.getAttribute("newRealEstateID");
            Calendar calendar = Calendar.getInstance();
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            int month = calendar.get(Calendar.MONTH);
            int year = calendar.get(Calendar.YEAR);
            //day + "/" + (month + 1) + "/" + year);
            startDate = day + "/" + (month + 1) + "/" + year;
            if (postTypeID == 1) {
                endDate = day + "/" + (month + 2) + "/" + year;
            } else if (postTypeID == 2) {
                endDate = day + "/" + (month + 3) + "/" + year;
            } else if(postTypeID == 3){
                endDate = day + "/" + (month + 4) + "/" + year;
            }
            flag = objPostHistoryDAO.insertPosHistory(userID, postTypeID, RealEstateID, startDate, endDate, postTypePrice);
            System.out.println("Flag: "+flag);
            if(flag){
                System.out.println("postTypePrice: "+postTypePrice);
                int price = Integer.parseInt(postTypePrice);
                int i = objUserDAO.updateMoney(userID, -price);
                return mapping.findForward(SUCCESS);
            }else{
                return mapping.findForward(ERROR);
            }
        } catch (Exception e) {
            return mapping.findForward(ERROR);
        }
    }
}